/*
 * MIT License
 *
 * Copyright (c) 2023 北京凯特伟业科技有限公司
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */
package com.je.message.service.lark;

/**
 * Api响应码枚举
 * <p>
 * 参见文档 https://open.feishu.cn/document/ukTMukTMukTM/ugjM14COyUjL4ITN
 *
 * @author wangmm@ketr.com.cn
 * @date 2020/3/31
 */
public enum Code {

    //响应码枚举
    SUCCESS(0, "接口调用成功"),
    _10002(10002, "发送消息失败"),
    _10003(10003, "请求参数不合法"),
    _10004(10004, "获取用户信息失败或者用户 ID 不存在"),
    _10005(10005, "生成 token 的 app_id 和相关 chat、open_id 的 app_id 不一致"),
    _10009(10009, "获取 open_chat_id 失败"),
    _10010(10010, "禁止发送消息，请检查 scope 权限，机器人可见性范围"),
    _10012(10012, "获取 app access token 失败"),
    _10013(10013, "获取 tenant access token 失败，可能是appid、app secret不正确等，具体信息请查看msg字段"),
    _10014(10014, "获取 tenant access token 失败，可能是appid、app secret不正确等，具体信息请查看msg字段"),
    _10015(10015, "app_secret 不正确"),
    _10016(10016, "发送 app_ticket 失败"),
    _10019(10019, "加急类型不支持"),
    _10020(10020, "消息 ID 不正确"),
    _10023(10023, "没有加急 scope 权限"),
    _10029(10029, "open_chat_id 不合法"),
    _10030(10030, "机器人不在群里"),
    _10032(10032, "所有 open_id 都不合法"),
    _10034(10034, "不支持跨企业操作"),
    _10037(10037, "获取 message_id 失败"),
    _11000(11000, "获取 sso_access_token 失败"),
    _11001(11001, "获取 CheckSecurityToken 失败"),
    _11100(11100, "open_chat_id不合法或者chat不存在"),
    _11101(11101, "open_id 不存在"),
    _11102(11102, "查询用户open_id 失败"),
    _11103(11103, "open_department_id 不存在"),
    _11104(11104, "查询用户 open_department_id 失败"),
    _11105(11105, "employee_id 不存在"),
    _11106(11106, "查询用户 employee_id 失败"),
    _11200(11200, "更新群名称失败"),
    _11201(11201, "机器人不是群主"),
    _11208(11208, "机器人不是群主"),
    _11202(11202, "只有群主才能拉用户进群"),
    _11203(11203, "机器人没有给用户批量发送权限"),
    _11204(11204, "机器人没有给部门批量发送权限"),
    _11205(11205, "应用没有机器人"),
    _11206(11206, "用户不在群中不能被设置为群主"),
    _11207(11207, "app不可用"),
    _11209(11209, "app不存在"),
    _11210(11210, "AppUsageInfo不存在"),
    _11211(11211, "拉人进群参数错误"),
    _11212(11212, "踢人出群参数错误"),
    _11213(11213, "更新群参数错误"),
    _11214(11214, "上传图片参数错误"),
    _11215(11215, "chat_id为空"),
    _11216(11216, "获取chat_id失败"),
    _11217(11217, "拉机器人进群失败"),
    _11218(11218, "群机器人已满"),
    _11219(11219, "不支持chat跨租户"),
    _11220(11220, "禁止机器人解散群"),
    _11221(11221, "机器人不能获取不属于自己的图片"),
    _11222(11222, "机器人的Owner不在群里"),
    _11223(11223, "没有打开应用发消息权限"),
    _11224(11224, "message id参数错误"),
    _11225(11225, "你的应用对用户不可见"),
    _11226(11226, "app id参数不对或者没传"),
    _11227(11227, "image_key不存在"),
    _11228(11228, "chat_id不合法"),
    _11229(11229, "无user scope权限"),
    _11230(11230, "无发送系统消息权限"),
    _11231(11231, "chat_id不存在"),
    _11232(11232, "发消息触发限流"),
    _11233(11233, "发消息触发限流"),
    _11234(11234, "bot非消息owner"),
    _11235(11235, "禁止@所有人"),
    _11236(11236, "用户已离职"),
    _11237(11237, "群聊已解散"),
    _11238(11238, "消息过久，不能撤销"),
    _11239(11239, "无权限获取"),
    over_frequency(99991400, "发消息频率超过频控限制，目前每个AppID每个接口50/s、1000/min的限制，超过这个频率的接口会返回99991400这个错误码。下一秒/下一分钟会自动恢复。"),
    tenant_access_token_invalid(99991663, "tenant access token 无效"),
    app_access_token_invalid(99991664, "app access token 无效"),
    tenant_code_invalid(99991665, "tenant code 无效"),
    app_ticket_invalid(99991666, "app ticket 无效"),
    INTERNAL(-1, "内部错误"),
    UNKNOWN(-1, "未知错误");

    /**
     * 响应码
     */
    private int code;

    /**
     * 响应码对应消息
     */
    private String msg;

    Code(int code, String msg) {
        this.code = code;
        this.msg = msg;
    }

    /**
     * 校验响应码对应的枚举
     *
     * @param code 响应码
     * @return com.je.auth.third.feishu.Code
     */
    public static Code check(int code) {

        if (code == 0) {
            // 0 为成功
            return SUCCESS;
        } else if (code >= 18000 && code <= 20000) {
            // 18000~20000 内部错误
            return INTERNAL;
        }
        //校验是否存在
        for (Code value : Code.values()) {
            if (value.getCode() == code) {
                return value;
            }
        }
        //无对应编码返回未知错误
        return UNKNOWN;
    }

    public int getCode() {
        return code;
    }

    public String getMsg() {
        return msg;
    }

    @Override
    public String toString() {
        return code + ":" + msg;
    }
}
